package com.bilin.coin.tools;

import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bilin.coin.R;
import com.bilin.coin.adapter.MyPagerAdapter;
import com.bilin.coin.common.base.BaseAppCompatActivity;
import com.bilin.coin.common.base.BaseFragment;
import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;

import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.viewpager.widget.ViewPager;
import butterknife.BindView;


public abstract class AbsViewPagerCustomTabActivity extends BaseAppCompatActivity implements SwipeRefreshLayout.OnRefreshListener {

    @BindView(R.id.viewPager)
    ViewPager viewPager;
    @BindView(R.id.tl_2)
    protected TabLayout tabLayout;
    @BindView(R.id.ll_head)
    LinearLayout ll_head;
    @BindView(R.id.sw_refresh)
    public SwipeRefreshLayout swRrefresh;
    @BindView(R.id.appbar_layout)
    AppBarLayout appBarLayout;

    protected ArrayList<BaseFragment> mFragmentList = new ArrayList<>();
    protected MyPagerAdapter pagerAdapter;


    @Override
    protected void initView() {
        fillFragment(mFragmentList);
        String[] mTitles = fillTabTitle();
        pagerAdapter = new MyPagerAdapter(getSupportFragmentManager(), mFragmentList, mTitles);
        viewPager.setAdapter(pagerAdapter);
        View inflate;
        for (int i = 0; i < mTitles.length; i++) {
            TabLayout.Tab tab = tabLayout.newTab();
            inflate = View.inflate(this, R.layout.item_tab_text, null);
            TextView textView = inflate.findViewById(R.id.tv_text);
            textView.setText(mTitles[i]);
            if (i == 0) {
                textView.setTextColor(getResources().getColor(R.color.colorPrimary));
            }
            tab.setCustomView(inflate);
            tabLayout.addTab(tab);
        }
        tabLayout.setSelectedTabIndicatorHeight(0);
        //tabLayout.setupWithViewPager(viewPager);
        /*if (mFragmentList.size() >= 5) {
            tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        } else {
            tabLayout.setTabMode(TabLayout.MODE_FIXED);
        }*/
        //tabLayout.setupWithViewPager(viewPager);

        ll_head.addView(fillHeadView(), new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));

        //设置样式刷新显示的位置
        swRrefresh.setProgressViewOffset(true, -20, 100);
        swRrefresh.setOnRefreshListener(this);
        //解决滑动冲突
        initEvent();
        swRrefresh.setEnabled(isCanRefresh());
    }

    public int currentPos;
    private void initEvent() {
        appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {

                if (isCanRefresh()) {
                    if (verticalOffset >= 0) {
                        swRrefresh.setEnabled(true);
                    } else {
                        swRrefresh.setEnabled(false);

                    }
                }
            }
        });
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int position = tab.getPosition();
                viewPager.setCurrentItem(position);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
                int position = tab.getPosition();
                if (position == 3) {
                    onTabReSelectedMy(tab);
                }
            }
        });
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                tabLayout.setScrollPosition(position, 0f, true);
                currentPos = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }




    protected void onTabSelectedMy(ImageView iv_up, ImageView iv_down){}

    protected void onTabReSelectedMy(TabLayout.Tab tab) {}

    protected boolean isCanRefresh() {
        return true;
    }

    protected abstract View fillHeadView();

    protected abstract String[] fillTabTitle();

    protected abstract void fillFragment(ArrayList<BaseFragment> mFragmentList);

    @Override
    protected void initData() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_common_viewpager_tab_cus;
    }

    @Override
    public void onRefresh() {
        refreshFragmentOrHead();
    }

    protected abstract void refreshFragmentOrHead();
}
